//
//  DateHelper.m
//  Rich
//
//  Created by Kenny Chen on 13/9/8.
//  Copyright (c) 2013年 Kenny Chen. All rights reserved.
//

#import "DateHelper.h"

@implementation DateHelper

+ (NSDate *)dateFor:(RichDateType)dateType byDate:(NSDate *)date {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    if (date == nil)
        date = [NSDate date];
    
    NSDateComponents *comps =
    [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                fromDate:date];
    
    if (dateType == RichDateYesterday) {
        comps.day--;
    }
    else if (dateType == RichDateFirstDayOfThisWeek) {
        comps.weekday = 1;
    }
    else if (dateType == RichDateFirstDayOfLastWeek) {
        comps.weekday = 1;
        comps.week--;
    }
    else if (dateType == RichDateSameDayOfLastWeek) {
        comps.week--;
    }
    else if (dateType == RichDateFirstDayOfThisMonth) {
        comps.day = 1;
    }
    else if (dateType == RichDateFirstDayOfLastMonth) {
        comps.day = 1;
        comps.month--;
    }
    else if (dateType == RichDateSameDayOfLastMonth) {
        comps.month--;
    }
    else if (dateType != RichDateToday)
        return nil;
    
    return [calendar dateFromComponents:comps];
}

+ (NSDate *)firstDayOfMonthForDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |
                                                                            NSMonthCalendarUnit |
                                                                            NSWeekCalendarUnit |
                                                                            NSDayCalendarUnit
                                                                   fromDate:date];
    components.day = 1;
    
    return [[[NSCalendar currentCalendar] dateFromComponents:components] copy];
}

+ (NSDate *)lastDayOfMonthForDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |
                                                                            NSMonthCalendarUnit |
                                                                            NSWeekCalendarUnit |
                                                                            NSDayCalendarUnit
                                                                   fromDate:date];
    components.month += 1;
    components.day = 0;
    
    return [[[NSCalendar currentCalendar] dateFromComponents:components] copy];
}

+ (NSDate *)firstDayOfWeekForDate:(NSDate *)date
{
    //NSLog(@"Input: %@", date);
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |
                                                                            NSMonthCalendarUnit |
                                                                            NSWeekdayCalendarUnit |
                                                                            NSDayCalendarUnit
                                                                   fromDate:date];
    components.weekday = 1;
    
    //NSLog(@"Output: %@", [[NSCalendar currentCalendar] dateFromComponents:components]);
    return [[[NSCalendar currentCalendar] dateFromComponents:components] copy];
}

+ (NSDate *)lastDayOfWeekForDate:(NSDate *)date
{
    NSDateComponents *componentsToAdd = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
                                                                        fromDate:[DateHelper firstDayOfWeekForDate:date]];
    [componentsToAdd setDay:6];
    
    return [[NSCalendar currentCalendar] dateByAddingComponents:componentsToAdd
                                                         toDate:[DateHelper firstDayOfWeekForDate:date]
                                                        options:0];
}

+ (NSInteger)numberOfWeeksOfMonthForDate:(NSDate *)date
{
    NSDateComponents *firstDateComponents;
    NSDateComponents *lastDateComponents;
    
    firstDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |
                                                                    NSMonthCalendarUnit |
                                                                    NSWeekCalendarUnit |
                                                                    NSDayCalendarUnit
                                                          fromDate:[DateHelper firstDayOfMonthForDate:date]];
    lastDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit |
                                                                    NSMonthCalendarUnit |
                                                                    NSWeekCalendarUnit |
                                                                    NSDayCalendarUnit
                                                         fromDate:[DateHelper lastDayOfMonthForDate:date]];
    
    return lastDateComponents.week - firstDateComponents.week + 1;
}

@end
